﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace WMS_Business
{
    public class HttpWeb
    {
        /// <summary>
        /// httpPost通讯
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bodydb"></param>
        /// <returns></returns>
        public string HttpPost(string url, string bodydb)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.Accept = "text/plain";
                request.ContentType = "application/json,multipart/form-data";

                byte[] buffer = Encoding.UTF8.GetBytes(bodydb);
                request.ContentLength = buffer.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                var re = request.GetResponse();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string json = reader.ReadToEnd();
                reader.Close();
                response.Close();
                return json;
            }
            catch (Exception ex)
            {
                return "http通讯异常" + ex.Message;
            }
        }
        /// <summary>
        /// httpGet通讯
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string HttpGet(string url)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.ContentType = "application/json";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return "http通讯异常" + ex.Message;
            }
        }
    }
}
